package com.xiaozhen.driver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaozhen.common.core.domain.driver.ParseDriver;
import com.xiaozhen.common.core.utils.StringUtils;
import com.xiaozhen.driver.dao.ParseDriverDao;
import com.xiaozhen.driver.service.ParseDriverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/11/3 10:32
 * @description:
 */
@Service
public class ParseDriverServiceImpl implements ParseDriverService {

    @Autowired
    private ParseDriverDao driverDao;

    @Override
    public List<ParseDriver> selectDriverList(ParseDriver param) {
        QueryWrapper<ParseDriver> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(param.getDriverType())) {
            queryWrapper.eq("driver_type", param.getDriverType());
        }
        return driverDao.selectList(queryWrapper);
    }

    @Override
    public Boolean checkDriverNameUnique(ParseDriver tcpDriver) {
        QueryWrapper<ParseDriver> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("driver_name", tcpDriver.getDriverName());
        List<ParseDriver> tcpDrivers = driverDao.selectList(queryWrapper);
        if (tcpDrivers.size() == 0) {
            return true;
        } else if (tcpDriver.getDriverId() != null && tcpDriver.getDriverId().equals(tcpDrivers.get(0).getDriverId())) {
            return true;
        }
        return false;
    }

    @Override
    public Integer insertDriver(ParseDriver tcpDriver) {
        return driverDao.insert(tcpDriver);
    }

    @Override
    public Integer updateDriver(ParseDriver tcpDriver) {
        return driverDao.updateById(tcpDriver);
    }

    @Override
    public Integer deleteDriverById(Long driverId) {
        return driverDao.deleteById(driverId);
    }
}
